/**
 * Created by Intellij IDEA
 *
 * @author 王俊凯
 * @Date: 2023/2/28 20:36
 * @Version 1.0
 */
package com.eight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.dao.CarShopDao;
import com.eight.pojo.BaseArea;
import com.eight.pojo.TbShop;
import com.eight.service.BaseAreaService;
import com.eight.service.CarShopService;
import com.eight.vo.CarVo;
import com.eight.vo.ComResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CarShopServiceImpl implements CarShopService {

    @Autowired
    private CarShopDao carShopDao;

    @Autowired
    private BaseAreaService baseAreaService;

    @Override
    public ComResult selectCarShop(CarVo carVo) {
        Page<TbShop> page = PageHelper.startPage(carVo.getPageNum(), carVo.getPageSize());
        QueryWrapper<TbShop> wrapper =null;
        if (carVo.getName()!=null){
            wrapper = new QueryWrapper<>();
            wrapper.like("name",carVo.getName());
        }
        List<TbShop> carShops = carShopDao.selectList(wrapper);
        System.out.println(carShops);
        if (carShops!=null){
            System.out.println("=============================");
            for (TbShop tbShop:carShops){
                QueryWrapper wrapper1=new QueryWrapper();
                wrapper1.eq("base_areaid",tbShop.getProvinceId());
                BaseArea list = baseAreaService.getOne(wrapper1);
                System.out.println(list);
                tbShop.setSheng(list);
            }
            for (TbShop tbShop:carShops){
                QueryWrapper wrapper1=new QueryWrapper();
                wrapper1.eq("base_areaid",tbShop.getCityId());
                BaseArea list = baseAreaService.getOne(wrapper1);
                tbShop.setCity(list);
            }
            for (TbShop tbShop:carShops){
                QueryWrapper wrapper1=new QueryWrapper();
                wrapper1.eq("base_areaid",tbShop.getAreaId());
                BaseArea list = baseAreaService.getOne(wrapper1);
                tbShop.setXian(list);
            }
        }
        PageInfo<TbShop> info = new PageInfo<>(carShops);
        return new ComResult(2000,"查询成功",info);
    }

    @Override
    public ComResult selectCarShop() {
        List<TbShop> tbShops = carShopDao.selectList(null);
        return new ComResult(2000,"查询成功",tbShops);
    }

    @Override
    public ComResult insertCarShop(TbShop tbShop) {
        int insert = carShopDao.insert(tbShop);
        return new ComResult(2000,"添加成功",insert);
    }

    @Override
    public ComResult deleteCarShop(int id) {
        int delete = carShopDao.deleteById(id);
        return new ComResult(2000,"删除成功",delete);
    }

    @Override
    public ComResult updateCarShop(TbShop tbShop) {
        int update = carShopDao.updateById(tbShop);
        return new ComResult(2000,"修改成功",update);
    }

    @Override
    public ComResult selectByAddress(TbShop tbShop) {
        QueryWrapper<TbShop> wrapper=new QueryWrapper();
        wrapper.eq("area_id",tbShop.getAreaId());
        wrapper.eq("city_id",tbShop.getCityId());
        wrapper.eq("province_id",tbShop.getProvinceId());
        List<TbShop> list = carShopDao.selectList(wrapper);
        return new ComResult(2000,"查询门店成功",list);
    }

    @Override
    public ComResult selectByCarName(Integer shopId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("shop_id",shopId);
        List<TbShop> tbShops = carShopDao.selectList(wrapper);
        return new ComResult(2000,"查询车辆成功",tbShops);
    }
}
